rm(list=ls())  
if(Sys.info()['user']=='dannychoi'){pathData<-"/Users/dannychoi/Dropbox/Projects/DCMPNS_Language/DataAnalysis"}
setwd(pathData)
library(stargazer)
library(estimatr)
library(readr)
library(lubridate)
library(tidyverse)
library(Rmisc)
library(lfe)
library(kableExtra)



# Importing merged data
load(file="merged.RData")

datafirst  <- merged[which(merged$exp==1), ]
datasecond <- merged[which(merged$exp==2), ]


##### II. Logistics and Procedures

# Figure S2: Study sites

##### Appendix Materials 

# Maps for the sites 

library(ggmap)
library(maptools)
library(sp)
library(rgdal)
library(tmap)
library(rgeos)
library(plyr)
library(ggrepel)

stat_west <- read.csv(file="map/StationList_west.csv")
stat_east <- read.csv(file="map/StationList_east.csv")

ogrListLayers("map/DEU_adm1.shp") #will show you available layers for the above dataset
germany<- readOGR("map/DEU_adm1.shp", layer="DEU_adm1")
germany@data$id <- rownames(germany@data)
germany.points = fortify(germany, region="id")
germany.df = join(germany.points, germany@data, by="id")
plot(germany)


germany.df$states <- 0
germany.df$states[germany.df$NAME_1=="Nordrhein-Westfalen"] <- 1
germany.df$states[germany.df$NAME_1=="Sachsen"] <- 1
germany.df$states[germany.df$NAME_1=="Brandenburg"] <- 1
germany.df$states[germany.df$NAME_1=="Niedersachsen"] <- 1

map <- ggplot() +
  geom_polygon(data = germany.df,
               aes(x = long, y = lat, group = group),
               color = 'grey', fill = 'white', size = 0.2, alpha=0.8) +
  geom_polygon(data = germany.df, aes(x = long, y = lat, fill=factor(states), alpha=0.2, group = group))+
  theme_void()

print(map)

map <- map + geom_point(aes(x=longitude, y=latitude), data=stat_west, size=1, color="darkblue", alpha=0.5)
map <- map + geom_point(aes(x=longitude, y=latitude), data=stat_east, size=1, color="darkblue", alpha=0.5)

print(map)

map <- map + geom_point(aes(x=longitude, y=latitude), data=stat_west, size=2.5, color="darkblue", alpha=0.5)
map <- map + geom_point(aes(x=longitude, y=latitude), data=stat_east, size=2.5, color="darkblue", alpha=0.5)
print(map)

map <- map + theme(legend.position="none")
map <- map + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank(),
                   axis.text.x=element_blank(), axis.ticks.x=element_blank())
map <- map + xlab(" ")
map <- map + ylab(" ")


print(map)

map <- map + theme(axis.title.x=element_blank(),
                   axis.text.x=element_blank(),
                   axis.ticks.x=element_blank(),
                   axis.title.y=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks.y=element_blank(),
                   panel.grid.major = element_blank(), 
                   panel.grid.minor = element_blank(),
                   panel.background = element_blank())

print(map)

ggsave("map/MapLanguage.png", width=4.1, height=5.3)


##### III. Descriptive Statistics
set.seed(1004)
library(Matching)

# Declaring covariates
covariates <- c("bystander", "hpprop", "femprop", "temp", "rush")
teststat <- c("mean.Tr", "mean.Co", "T pval")
sumstat <- merged[covariates]

# Descriptive Statistics
stargazer(sumstat, type="latex")



##### IV. Covariate Balance

## Figure 2 balance tests 

library(Matching)
library(ebal)

# Hijab vs native
mb_hvn <- MatchBalance(hvn ~ bystander + hpprop + femprop + temp + rush, data=merged, match.out=NULL, nboots=5000)
mb_hvn <- ebal::baltest.collect(mb_hvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hvn <- as.data.frame(mb_hvn)
mb_hvn <- mb_hvn[teststat] # coercing into dataframe

kable(mb_hvn, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


# Hijab vs no hijab
mb_hvnh <- MatchBalance(hvnh ~ bystander+hpprop+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hvnh <- ebal::baltest.collect(mb_hvnh, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hvnh <- as.data.frame(mb_hvnh)
mb_hvnh <- mb_hvnh[teststat] # coercing into dataframe

kable(mb_hvnh, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


# No hijab vs native
mb_nhvn <- MatchBalance(nhvn ~ bystander+hpprop+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_nhvn <- ebal::baltest.collect(mb_nhvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_nhvn <- as.data.frame(mb_nhvn)
mb_nhvn <- mb_nhvn[teststat] # coercing into dataframe

kable(mb_nhvn, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


## Figure 3 balance tests 

# Hijab foreign language vs hijab German language
mb_hfvhg <- MatchBalance(hfvhg ~ bystander+hpprop+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hfvhg <- ebal::baltest.collect(mb_hfvhg, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hfvhg <- as.data.frame(mb_hfvhg)
mb_hfvhg <- mb_hfvhg[teststat] # coercing into dataframe

kable(mb_hfvhg, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


# No hijab foreign language vs no hijab German language
mb_nfvng <- MatchBalance(nfvng ~ bystander+hpprop+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_nfvng <- ebal::baltest.collect(mb_nfvng, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_nfvng <- as.data.frame(mb_nfvng)
mb_nfvng <- mb_nfvng[teststat] # coercing into dataframe

kable(mb_nfvng, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


# Hijab German language vs native
mb_hgvn <- MatchBalance(hgvn ~ bystander+hpprop+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hgvn <- ebal::baltest.collect(mb_hgvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hgvn <- as.data.frame(mb_hgvn)
mb_hgvn <- mb_hgvn[teststat] # coercing into dataframe

kable(mb_hgvn, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


# Hijab German language vs native
mb_hgvn <- MatchBalance(hgvn ~ bystander+femprop+temp+rush, data=merged, match.out=NULL, nboots=5000)
mb_hgvn <- ebal::baltest.collect(mb_hgvn, var.names=covariates, after = FALSE) # compiling balance statistics 
mb_hgvn <- as.data.frame(mb_hgvn)
mb_hgvn <- mb_hgvn[teststat] # coercing into dataframe

kable(mb_hgvn, "latex", booktabs=TRUE) %>%
  kable_styling(position="center")


##### V. Additional Analyses

# Figure S3: Offsetting effects - Experiment 1
langofftest1 <- Rmisc::summarySE(datafirst, measurevar="help", groupvars=c("languageoff"))
langofftest1$point <- sprintf(langofftest1$help*100, fmt = '%#.2f')

interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=111)", "(2)\nHijab\nGerman Language\n(N=119)", "(3)\nNo Hijab\nForeign Language\n(N=116)",
              "(4)\nNo Hijab\nGerman Language\n(N=122)", "(5)\nNative\nGerman Language\n(N=120)")


pd <- position_dodge(0.1)
pdf("langoffset_iteration_exp1.pdf", width = 8, height = 5)
langoffset1 <- ggplot(langofftest1, aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=11), axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=11), axis.ticks.x=element_line(colour="gray30"))
langoffset1<- langoffset1 + geom_segment(aes(x=1, y=87, xend=1.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.2, y=90, xend=1.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.8, y=90, xend=2, yend=87), colour="darkgray")
langoffset1 <- langoffset1 + annotate("text", x=1.5, y=93, label="5.3%p (p=0.408)", size=3)
langoffset1<- langoffset1 + geom_segment(aes(x=3, y=93, xend=3.2, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.2, y=96, xend=3.8, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.8, y=96, xend=4, yend=93), colour="darkgray")
langoffset1 <- langoffset1 + annotate("text", x=3.5, y=99, label="1.1%p (p=0.834)", size=3)
langoffset1<- langoffset1 + geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
langoffset1 <- langoffset1 + annotate("text", x=3.5, y=109, label="10.3%p (p=0.0878)", size=3)
langoffset1 <- langoffset1 + scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
langoffset1 <- langoffset1 + theme(legend.position="none")
print(langoffset1)
dev.off()


# Figure S4: Offsetting effects - Experiment 2 
langofftest <- Rmisc::summarySE(datasecond, measurevar="help", groupvars=c("languageoff"))
langofftest$point <- sprintf(langofftest$help*100, fmt = '%#.2f')

interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=173)", "(2)\nHijab\nGerman Language\n(N=215)", "(3)\nNo Hijab\nForeign Language\n(N=193)",
              "(4)\nNo Hijab\nGerman Language\n(N=207)", "(5)\nNative\nGerman Language\n(N=192)")

pd <- position_dodge(0.1)
pdf("langoffset_iteration_exp2.pdf", width = 8, height = 5)
langoffset2 <- ggplot(langofftest, aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=11), axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=11), axis.ticks.x=element_line(colour="gray30"))
langoffset2<- langoffset2 + geom_segment(aes(x=1, y=87, xend=1.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.2, y=90, xend=1.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=1.8, y=90, xend=2, yend=87), colour="darkgray")
langoffset2 <- langoffset2 + annotate("text", x=1.5, y=93, label="-3.5%p (p=0.456)", size=3)
langoffset2<- langoffset2 + geom_segment(aes(x=3, y=93, xend=3.2, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.2, y=96, xend=3.8, yend=96), colour="darkgray") +
  geom_segment(aes(x=3.8, y=96, xend=4, yend=93), colour="darkgray")
langoffset2 <- langoffset2 + annotate("text", x=3.5, y=99, label="4.5%p (p=0.274)", size=3)
langoffset2<- langoffset2 + geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
langoffset2 <- langoffset2 + annotate("text", x=3.5, y=109, label="9.1%p (p=0.0438)", size=3)
langoffset2 <- langoffset2 + scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
langoffset2 <- langoffset2 + theme(legend.position="none")
print(langoffset2)
dev.off()


# Figure S5/S6: Discrimination plot for West and East Germany

# Subsetting Data to West/East Germany
table(merged$team)
merged$region <- NA

# region==1 if West germany, region==2 if East Germany
merged$region <- ifelse(merged$team=="NRW1" | merged$team=="NRW2"  | merged$team=="NRW3" |
                        merged$team=="1" | merged$team=="2" | merged$team=="3" | merged$team=="4" , 1, 2)
table(merged$region)

# Subsetting data
west <- merged[which(merged$region==1), ]
east <- merged[which(merged$region==2), ]


# Figure S5: West Germany
mergedofftestw <- Rmisc::summarySE(west, measurevar="help", groupvars=c("languageoff"))
mergedofftestw$point <- sprintf(mergedofftestw$help*100, fmt = '%#.2f')


interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=159)", "(2)\nHijab\nGerman Language\n(N=213)", "(3)\nNo Hijab\nForeign Language\n(N=178)",
              "(4)\nNo Hijab\nGerman Language\n(N=206)", "(5)\nNative\nGerman Language\n(N=200)")


pd <- position_dodge(0.1)
pdf("langoffset_merged_west.pdf", width = 8, height = 5)
mergedoffsetw <- ggplot(mergedofftestw, aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=11), axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=11), axis.ticks.x=element_line(colour="gray30"))
mergedoffsetw<- mergedoffsetw + geom_segment(aes(x=1, y=80, xend=1.2, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.2, y=83, xend=1.8, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.8, y=83, xend=2, yend=80), colour="darkgray")
mergedoffsetw <- mergedoffsetw + annotate("text", x=1.5, y=86, label="-0.06%p (p=0.893)", size=3)
mergedoffsetw<- mergedoffsetw + geom_segment(aes(x=3, y=87, xend=3.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.2, y=90, xend=3.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.8, y=90, xend=4, yend=87), colour="darkgray")
mergedoffsetw <- mergedoffsetw + annotate("text", x=3.5, y=93, label="2.6%p (p=0.540)", size=3)
mergedoffsetw<- mergedoffsetw + geom_segment(aes(x=2, y=95, xend=2.2, yend=98), colour="darkgray") +
  geom_segment(aes(x=2.2, y=98, xend=3.8, yend=98), colour="darkgray") +
  geom_segment(aes(x=3.8, y=98, xend=4, yend=95), colour="darkgray")
mergedoffsetw <- mergedoffsetw + annotate("text", x=3, y=101, label="9.7%p (p=0.022)", size=3)
mergedoffsetw<- mergedoffsetw + geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
mergedoffsetw <- mergedoffsetw + annotate("text", x=3.5, y=109, label="3.1%p (p=0.488)", size=3)
mergedoffsetw <- mergedoffsetw + scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
mergedoffsetw <- mergedoffsetw + theme(legend.position="none")
print(mergedoffsetw)
dev.off()


# Figure S6: East Germany
mergedoffteste <- Rmisc::summarySE(east, measurevar="help", groupvars=c("languageoff"))
mergedoffteste$point <- sprintf(mergedoffteste$help*100, fmt = '%#.2f')


interval1 <- -qnorm((1-0.90)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
mylabels <- c("(1)\nHijab\nForeign Language\n(N=125)", "(2)\nHijab\nGerman Language\n(N=121)", "(3)\nNo Hijab\nForeign Language\n(N=131)",
              "(4)\nNo Hijab\nGerman Language\n(N=123)", "(5)\nNative\nGerman Language\n(N=112)")


pd <- position_dodge(0.1)
pdf("langoffset_merged_east.pdf", width = 8, height = 5)
mergedoffsete <- ggplot(mergedoffteste, aes(x=factor(languageoff), y=100*help, fill=factor(languageoff))) +
  geom_bar(position=pd, stat="identity", width=0.45, alpha=0.9) +
  geom_errorbar(aes(ymin = 100*(help - se*interval2), ymax = 100*(help + se*interval2)),
                width = 0.1, position = position_dodge(width = 1/2)) +
  geom_text(aes(label = point), size = 4.0, vjust=-0.5, hjust = -0.4, nudge_x = 0.05) +
  ggtitle(" ") +
  xlab("") +
  ylab("Assistance Rates (%)") +
  ylim(0.0, 110) +
  scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=mylabels)+
  theme_minimal() +
  theme(axis.text.y=element_text(colour = "gray30", size=11), axis.ticks.y=element_blank()) +
  theme(axis.text.x=element_text(colour = "gray30", size=11), axis.ticks.x=element_line(colour="gray30"))
mergedoffsete<- mergedoffsete + geom_segment(aes(x=1, y=80, xend=1.2, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.2, y=83, xend=1.8, yend=83), colour="darkgray") +
  geom_segment(aes(x=1.8, y=83, xend=2, yend=80), colour="darkgray")
mergedoffsete <- mergedoffsete + annotate("text", x=1.5, y=86, label="-1.4%p (p=0.828)", size=3)
mergedoffsete<- mergedoffsete + geom_segment(aes(x=3, y=87, xend=3.2, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.2, y=90, xend=3.8, yend=90), colour="darkgray") +
  geom_segment(aes(x=3.8, y=90, xend=4, yend=87), colour="darkgray")
mergedoffsete <- mergedoffsete + annotate("text", x=3.5, y=93, label="4.0%p (p=0.457)", size=3)
mergedoffsete<- mergedoffsete + geom_segment(aes(x=2, y=95, xend=2.2, yend=98), colour="darkgray") +
  geom_segment(aes(x=2.2, y=98, xend=3.8, yend=98), colour="darkgray") +
  geom_segment(aes(x=3.8, y=98, xend=4, yend=95), colour="darkgray")
mergedoffsete <- mergedoffsete + annotate("text", x=3, y=101, label="21.0%p (p=0.0004)", size=3)
mergedoffsete<- mergedoffsete + geom_segment(aes(x=2, y=103, xend=2.2, yend=106), colour="darkgray") +
  geom_segment(aes(x=2.2, y=106, xend=4.8, yend=106), colour="darkgray") +
  geom_segment(aes(x=4.8, y=106, xend=5, yend=103), colour="darkgray")
mergedoffsete <- mergedoffsete + annotate("text", x=3.5, y=109, label="20.7%p (p=0.0008)", size=3)
mergedoffsete <- mergedoffsete + scale_fill_manual(values=c("#CC0000","#FF0000","#CC33CC","#9900FF", "#0000FF"))
mergedoffsete <- mergedoffsete + theme(legend.position="none")
print(mergedoffsete)
dev.off()


##### VI. Equivalence Tests 

library(TOSTER) # For implementing two one-sided tests of proportions

# Create treatment variable for test 1: immigrant foreign (hijab, no hijab pooled) vs immigrant German

merged$ifvig <- NA
merged$ifvig[merged$languageoff==1 | merged$languageoff==3] <- 1
merged$ifvig[merged$languageoff==2 | merged$languageoff==4] <- 0

langtesteq  <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("ifvig"))
langtesteq2 <- Rmisc::summarySE(merged, measurevar="help", groupvars=c("hfvhg"))


TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.04, high_eqbound=0.04, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.04, high_eqbound=0.04, alpha = 0.05)

TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.06, high_eqbound=0.06, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.06, high_eqbound=0.06, alpha = 0.05)

TOSTtwo.prop(0.7239, 0.7099,  n1=663, n2=593, 
             low_eqbound=-0.08, high_eqbound=0.08, alpha = 0.05)
TOSTtwo.prop(0.6556, 0.65499, n1=334, n2=284, 
             low_eqbound=-0.08, high_eqbound=0.08, alpha = 0.05)



##### VII. Effects by Foreign Language Used

# Coding Turkish vs Arabic for foreign language used
merged$turkish <- 0
merged$turkish[merged$team=="Bburg1" | merged$team=="NRW3" | merged$team=="Sachsen1" |
               merged$team=="1" | merged$team=="2" | merged$team=="4" | merged$team=="6"] <- 1


# Table S5: Effects by foreign language used

turkish  <- lm(help~hfvhg, data=subset(merged, turkish==1))
arabic   <- lm(help~hfvhg, data=subset(merged, turkish==0))

bothlang <- lm(help~hfvhg*turkish, data=merged)



stargazer(turkish, arabic, bothlang, 
          dep.var.labels  = c("Any help?"),
          type="text", 
          omit.stat = c("f"),
          keep=c("hfvhg", "turkish", "hfvhg:turkish", "Constant"),
          keep.stat=c("n", "rsq"),
          add.lines = list(c("Foreign Language", "Turkish", "Arabic", "Merged")))



##### VIII. Manipulation Checks on Perception of Confederate Ethnicity

# Import manipulation check data set 
mcheck <- read.csv(file="mcheck.csv")

# Coding whether respondents who classified the photo as "native German"
mcheck$q1 <- ifelse(mcheck$Q1==1, 1, 0)
mcheck$q2 <- ifelse(mcheck$Q2==1, 1, 0)
mcheck$q3 <- ifelse(mcheck$Q3==1, 1, 0)
mcheck$q4 <- ifelse(mcheck$Q4==1, 1, 0)
mcheck$q5 <- ifelse(mcheck$Q5==1, 1, 0)
mcheck$q6 <- ifelse(mcheck$Q6==1, 1, 0)
mcheck$q7 <- ifelse(mcheck$Q7==1, 1, 0)
mcheck$q8 <- ifelse(mcheck$Q8==1, 1, 0)
mcheck$q9 <- ifelse(mcheck$Q9==1, 1, 0)
mcheck$q10 <- ifelse(mcheck$Q10==1, 1, 0)
mcheck$q11 <- ifelse(mcheck$Q11==1, 1, 0)
mcheck$q12 <- ifelse(mcheck$Q12==1, 1, 0)
mcheck$q13 <- ifelse(mcheck$Q13==1, 1, 0)
mcheck$q14 <- ifelse(mcheck$Q14==1, 1, 0)
mcheck$q15 <- ifelse(mcheck$Q15==1, 1, 0)

# Classifying photos of native vs immigrant confederates
native <- c("q1","q2", "q5", "q6", "q9", "q12", "q13", "q14")
immigrant <- c("q3", "q4", "q7", "q8", "q10", "q15")

checknative <- mcheck[native] 
checknative <- checknative[complete.cases(checknative), ]

checkimm <- mcheck[immigrant]
checkimm <- checkimm[complete.cases(checknative), ]

library(tidyr)

# Collapsing all native vs immigrant 
checkn_long <- checknative %>%
  gather(item, percent, q1:q14)
checkn_long$category <- "native"

mean(checkn_long$percent, na.rm=T)

checkimm <- mcheck[immigrant]
checki_long <- checkimm %>%
  gather(item, percent, q3:q15)
checki_long$category <- "immigrant"

mean(checki_long$percent, na.rm=T)

# Creating merged data frame
manipcheck1 <- rbind(checkn_long, checki_long)

# Implement t-test
t.test(manipcheck1$percent ~ manipcheck1$category)

